
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>pprint --- 数据美化输出 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="reprlib --- 另一种 repr() 实现" href="reprlib.html" />
    <link rel="prev" title="copy --- 浅层 (shallow) 和深层 (deep) 复制操作" href="copy.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/pprint.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="reprlib.html" title="reprlib --- 另一种 repr() 实现"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="copy.html" title="copy --- 浅层 (shallow) 和深层 (deep) 复制操作"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">数据类型</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-pprint">
<span id="pprint-data-pretty-printer"></span><h1><a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code></a> --- 数据美化输出<a class="headerlink" href="#module-pprint" title="永久链接至标题">¶</a></h1>
<p><strong>源代码：</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/pprint.py">Lib/pprint.py</a></p>
<hr class="docutils" />
<p><a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code></a> 模块提供了“美化打印”任意 Python 数据结构的功能，这种美化形式可用作对解释器的输入。 如果经格式化的结构包含非基本 Python 类型的对象，则其美化形式可能无法被加载。 包含文件、套接字或类对象，以及许多其他不能用 Python 字面值来表示的对象都有可能导致这样的结果。</p>
<p>格式化后的形式会在可能的情况下以单行来表示对象，并在无法在允许宽度内容纳对象的情况下将其分为多行。 如果你需要调整宽度限制则应显式地构造 <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> 对象。</p>
<p>Dictionaries are sorted by key before the display is computed.
字典在被计算前通过键值来排序。</p>
<p><a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code></a> 模块定义了一个类：</p>
<span class="target" id="index-0"></span><dl class="class">
<dt id="pprint.PrettyPrinter">
<em class="property">class </em><code class="sig-prename descclassname">pprint.</code><code class="sig-name descname">PrettyPrinter</code><span class="sig-paren">(</span><em class="sig-param">indent=1</em>, <em class="sig-param">width=80</em>, <em class="sig-param">depth=None</em>, <em class="sig-param">stream=None</em>, <em class="sig-param">*</em>, <em class="sig-param">compact=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter" title="永久链接至目标">¶</a></dt>
<dd><p>构造一个 <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> 实例。 此构造器接受几个关键字形参。 使用 <em>stream</em> 关键字可设置输出流；在流对象上使用的唯一方法是文件协议的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> 方法。 如果未指定此关键字，则 <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> 会选择 <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>。 每个递归层级的缩进量由 <em>indent</em> 指定；默认值为一。 其他值可导致输出看起来有些怪异，但可使得嵌套结构更易区分。 可被打印的层级数量由 <em>depth</em> 控制；如果数据结构的层级被打印得过深，其所包含的下一层级会被替换为 <code class="docutils literal notranslate"><span class="pre">...</span></code>。 在默认情况下，对被格式化对象的层级深度没有限制。 希望的输出宽度可使用 <em>width</em> 形参来限制；默认值为 80 个字符。 如果一个结构无法在限定宽度内被格式化，则将做到尽可能接近。 如果 <em>compact</em> 为假值（默认）则长序列的每一项将被格式化为单独的行。 如果 <em>compact</em> 为真值，则格式化将在 <em>width</em> 可容纳的情况下把尽可能多的项放入每个输出行。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>加入*compact* 参数。</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pprint</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stuff</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="s1">&#39;eggs&#39;</span><span class="p">,</span> <span class="s1">&#39;lumberjack&#39;</span><span class="p">,</span> <span class="s1">&#39;knights&#39;</span><span class="p">,</span> <span class="s1">&#39;ni&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stuff</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">stuff</span><span class="p">[:])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span> <span class="o">=</span> <span class="n">pprint</span><span class="o">.</span><span class="n">PrettyPrinter</span><span class="p">(</span><span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
<span class="go">[   [&#39;spam&#39;, &#39;eggs&#39;, &#39;lumberjack&#39;, &#39;knights&#39;, &#39;ni&#39;],</span>
<span class="go">    &#39;spam&#39;,</span>
<span class="go">    &#39;eggs&#39;,</span>
<span class="go">    &#39;lumberjack&#39;,</span>
<span class="go">    &#39;knights&#39;,</span>
<span class="go">    &#39;ni&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span> <span class="o">=</span> <span class="n">pprint</span><span class="o">.</span><span class="n">PrettyPrinter</span><span class="p">(</span><span class="n">width</span><span class="o">=</span><span class="mi">41</span><span class="p">,</span> <span class="n">compact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
<span class="go">[[&#39;spam&#39;, &#39;eggs&#39;, &#39;lumberjack&#39;,</span>
<span class="go">  &#39;knights&#39;, &#39;ni&#39;],</span>
<span class="go"> &#39;spam&#39;, &#39;eggs&#39;, &#39;lumberjack&#39;, &#39;knights&#39;,</span>
<span class="go"> &#39;ni&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tup</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;eggs&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;lumberjack&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;knights&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;ni&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;dead&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">(</span><span class="s1">&#39;parrot&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;fresh fruit&#39;</span><span class="p">,))))))))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span> <span class="o">=</span> <span class="n">pprint</span><span class="o">.</span><span class="n">PrettyPrinter</span><span class="p">(</span><span class="n">depth</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">tup</span><span class="p">)</span>
<span class="go">(&#39;spam&#39;, (&#39;eggs&#39;, (&#39;lumberjack&#39;, (&#39;knights&#39;, (&#39;ni&#39;, (&#39;dead&#39;, (...)))))))</span>
</pre></div>
</div>
</div>
</dd></dl>

<p><a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code></a> 模块还提供了一些快捷函数：</p>
<dl class="function">
<dt id="pprint.pformat">
<code class="sig-prename descclassname">pprint.</code><code class="sig-name descname">pformat</code><span class="sig-paren">(</span><em class="sig-param">object</em>, <em class="sig-param">indent=1</em>, <em class="sig-param">width=80</em>, <em class="sig-param">depth=None</em>, <em class="sig-param">*</em>, <em class="sig-param">compact=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.pformat" title="永久链接至目标">¶</a></dt>
<dd><p>将 <em>object</em> 格式化表示作为字符串返回。 <em>indent</em>, <em>width</em>, <em>depth</em> 和 <em>compact</em> 将作为格式化形参被传入 <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> 构造器。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>加入*compact* 参数。</p>
</div>
</dd></dl>

<dl class="function">
<dt id="pprint.pprint">
<code class="sig-prename descclassname">pprint.</code><code class="sig-name descname">pprint</code><span class="sig-paren">(</span><em class="sig-param">object</em>, <em class="sig-param">stream=None</em>, <em class="sig-param">indent=1</em>, <em class="sig-param">width=80</em>, <em class="sig-param">depth=None</em>, <em class="sig-param">*</em>, <em class="sig-param">compact=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.pprint" title="永久链接至目标">¶</a></dt>
<dd><p>在 <em>stream</em> 上打印 <em>object</em> 的格式化表示，并附带一个换行符。 如果 <em>stream</em> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则使用 <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>。 这可以替换 <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> 函数在交互式解释器中使用以查看值（你甚至可以执行重新赋值 <code class="docutils literal notranslate"><span class="pre">print</span> <span class="pre">=</span> <span class="pre">pprint.pprint</span></code> 以在特定作用域中使用）。 <em>indent</em>, <em>width</em>, <em>depth</em> 和 <em>compact</em> 将作为格式化形参被传给 <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> 构造器。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>加入*compact* 参数。</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pprint</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stuff</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="s1">&#39;eggs&#39;</span><span class="p">,</span> <span class="s1">&#39;lumberjack&#39;</span><span class="p">,</span> <span class="s1">&#39;knights&#39;</span><span class="p">,</span> <span class="s1">&#39;ni&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stuff</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">stuff</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
<span class="go">[&lt;Recursion on list with id=...&gt;,</span>
<span class="go"> &#39;spam&#39;,</span>
<span class="go"> &#39;eggs&#39;,</span>
<span class="go"> &#39;lumberjack&#39;,</span>
<span class="go"> &#39;knights&#39;,</span>
<span class="go"> &#39;ni&#39;]</span>
</pre></div>
</div>
</div>
</dd></dl>

<dl class="function">
<dt id="pprint.isreadable">
<code class="sig-prename descclassname">pprint.</code><code class="sig-name descname">isreadable</code><span class="sig-paren">(</span><em class="sig-param">object</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.isreadable" title="永久链接至目标">¶</a></dt>
<dd><p id="index-1">确定 <em>object</em> 的格式化表示是否“可读”，或是否可被用来通过 <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> 重新构建对象的值。 此函数对于递归对象总是返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">isreadable</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="pprint.isrecursive">
<code class="sig-prename descclassname">pprint.</code><code class="sig-name descname">isrecursive</code><span class="sig-paren">(</span><em class="sig-param">object</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.isrecursive" title="永久链接至目标">¶</a></dt>
<dd><p>确定 <em>object</em> 是否需要递归表示。</p>
</dd></dl>

<p>此外还定义了一个支持函数：</p>
<dl class="function">
<dt id="pprint.saferepr">
<code class="sig-prename descclassname">pprint.</code><code class="sig-name descname">saferepr</code><span class="sig-paren">(</span><em class="sig-param">object</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.saferepr" title="永久链接至目标">¶</a></dt>
<dd><p>返回 <em>object</em> 的字符串表示，并为递归数据结构提供保护。 如果 <em>object</em> 的表示形式公开了一个递归条目，该递归引用会被表示为 <code class="docutils literal notranslate"><span class="pre">&lt;Recursion</span> <span class="pre">on</span> <span class="pre">typename</span> <span class="pre">with</span> <span class="pre">id=number&gt;</span></code>。 该表示因而不会进行其它格式化。</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">saferepr</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
<span class="go">&quot;[&lt;Recursion on list with id=...&gt;, &#39;spam&#39;, &#39;eggs&#39;, &#39;lumberjack&#39;, &#39;knights&#39;, &#39;ni&#39;]&quot;</span>
</pre></div>
</div>
</dd></dl>

<div class="section" id="prettyprinter-objects">
<span id="id1"></span><h2>PrettyPrinter 对象<a class="headerlink" href="#prettyprinter-objects" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> 的实例具有下列方法：</p>
<dl class="method">
<dt id="pprint.PrettyPrinter.pformat">
<code class="sig-prename descclassname">PrettyPrinter.</code><code class="sig-name descname">pformat</code><span class="sig-paren">(</span><em class="sig-param">object</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter.pformat" title="永久链接至目标">¶</a></dt>
<dd><p>返回 <em>object</em> 格式化表示。 这会将传给 <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> 构造器的选项纳入考虑。</p>
</dd></dl>

<dl class="method">
<dt id="pprint.PrettyPrinter.pprint">
<code class="sig-prename descclassname">PrettyPrinter.</code><code class="sig-name descname">pprint</code><span class="sig-paren">(</span><em class="sig-param">object</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter.pprint" title="永久链接至目标">¶</a></dt>
<dd><p>在所配置的流上打印 <em>object</em> 的格式化表示，并附加一个换行符。</p>
</dd></dl>

<p>下列方法提供了与同名函数相对应的实现。 在实例上使用这些方法效率会更高一些，因为不需要创建新的 <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> 对象。</p>
<dl class="method">
<dt id="pprint.PrettyPrinter.isreadable">
<code class="sig-prename descclassname">PrettyPrinter.</code><code class="sig-name descname">isreadable</code><span class="sig-paren">(</span><em class="sig-param">object</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter.isreadable" title="永久链接至目标">¶</a></dt>
<dd><p id="index-2">确定对象的格式化表示是否“可读”，或者是否可使用 <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> 重建对象值。 请注意此方法对于递归对象将返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。 如果设置了 <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> 的 <em>depth</em> 形参并且对象深度超出允许范围，此方法将返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="pprint.PrettyPrinter.isrecursive">
<code class="sig-prename descclassname">PrettyPrinter.</code><code class="sig-name descname">isrecursive</code><span class="sig-paren">(</span><em class="sig-param">object</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter.isrecursive" title="永久链接至目标">¶</a></dt>
<dd><p>确定对象是否需要递归表示。</p>
</dd></dl>

<p>此方法作为一个钩子提供，允许子类修改将对象转换为字符串的方式。 默认实现使用 <a class="reference internal" href="#pprint.saferepr" title="pprint.saferepr"><code class="xref py py-func docutils literal notranslate"><span class="pre">saferepr()</span></code></a> 实现的内部方式。</p>
<dl class="method">
<dt id="pprint.PrettyPrinter.format">
<code class="sig-prename descclassname">PrettyPrinter.</code><code class="sig-name descname">format</code><span class="sig-paren">(</span><em class="sig-param">object</em>, <em class="sig-param">context</em>, <em class="sig-param">maxlevels</em>, <em class="sig-param">level</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter.format" title="永久链接至目标">¶</a></dt>
<dd><p>返回三个值：字符串形式的 <em>object</em> 已格式化版本，指明结果是否可读的旗标，以及指明是否检测到递归的旗标。 第一个参数是要表示的对象。 第二个是以对象 <a class="reference internal" href="functions.html#id" title="id"><code class="xref py py-func docutils literal notranslate"><span class="pre">id()</span></code></a> 为键的字典，这些对象是当前表示上下文的一部分（影响 <em>object</em> 表示的直接和间接容器）；如果需要呈现一个已经在 <em>context</em> 中表示的对象，则第三个返回值应当为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。 对 <a class="reference internal" href="#pprint.PrettyPrinter.format" title="pprint.PrettyPrinter.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> 方法的递归调用应当将容器的附加条目添加到此字典中。 第三个参数 <em>maxlevels</em> 给出了对递归的请求限制；如果没有请求限制则其值将为 <code class="docutils literal notranslate"><span class="pre">0</span></code>。 此参数应当不加修改地传给递归调用。 第四个参数 <em>level</em> 给出于当前层级；传给递归调用的参数值应当小于当前调用的值。</p>
</dd></dl>

</div>
<div class="section" id="example">
<span id="pprint-example"></span><h2>示例<a class="headerlink" href="#example" title="永久链接至标题">¶</a></h2>
<p>为了演示 <a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-func docutils literal notranslate"><span class="pre">pprint()</span></code></a> 函数及其形参的几种用法，让我们从 <a class="reference external" href="https://pypi.org">PyPI</a> 获取关于某个项目的信息:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pprint</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">urllib.request</span> <span class="kn">import</span> <span class="n">urlopen</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">urlopen</span><span class="p">(</span><span class="s1">&#39;https://pypi.org/pypi/sampleproject/json&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">resp</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">project_info</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">resp</span><span class="p">)[</span><span class="s1">&#39;info&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p><a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-func docutils literal notranslate"><span class="pre">pprint()</span></code></a> 以其基本形式显示了整个对象:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">project_info</span><span class="p">)</span>
<span class="go">{&#39;author&#39;: &#39;The Python Packaging Authority&#39;,</span>
<span class="go"> &#39;author_email&#39;: &#39;pypa-dev@googlegroups.com&#39;,</span>
<span class="go"> &#39;bugtrack_url&#39;: None,</span>
<span class="go"> &#39;classifiers&#39;: [&#39;Development Status :: 3 - Alpha&#39;,</span>
<span class="go">                 &#39;Intended Audience :: Developers&#39;,</span>
<span class="go">                 &#39;License :: OSI Approved :: MIT License&#39;,</span>
<span class="go">                 &#39;Programming Language :: Python :: 2&#39;,</span>
<span class="go">                 &#39;Programming Language :: Python :: 2.6&#39;,</span>
<span class="go">                 &#39;Programming Language :: Python :: 2.7&#39;,</span>
<span class="go">                 &#39;Programming Language :: Python :: 3&#39;,</span>
<span class="go">                 &#39;Programming Language :: Python :: 3.2&#39;,</span>
<span class="go">                 &#39;Programming Language :: Python :: 3.3&#39;,</span>
<span class="go">                 &#39;Programming Language :: Python :: 3.4&#39;,</span>
<span class="go">                 &#39;Topic :: Software Development :: Build Tools&#39;],</span>
<span class="go"> &#39;description&#39;: &#39;A sample Python project\n&#39;</span>
<span class="go">                &#39;=======================\n&#39;</span>
<span class="go">                &#39;\n&#39;</span>
<span class="go">                &#39;This is the description file for the project.\n&#39;</span>
<span class="go">                &#39;\n&#39;</span>
<span class="go">                &#39;The file should use UTF-8 encoding and be written using &#39;</span>
<span class="go">                &#39;ReStructured Text. It\n&#39;</span>
<span class="go">                &#39;will be used to generate the project webpage on PyPI, and &#39;</span>
<span class="go">                &#39;should be written for\n&#39;</span>
<span class="go">                &#39;that purpose.\n&#39;</span>
<span class="go">                &#39;\n&#39;</span>
<span class="go">                &#39;Typical contents for this file would include an overview of &#39;</span>
<span class="go">                &#39;the project, basic\n&#39;</span>
<span class="go">                &#39;usage examples, etc. Generally, including the project &#39;</span>
<span class="go">                &#39;changelog in here is not\n&#39;</span>
<span class="go">                &#39;a good idea, although a simple &quot;What\&#39;s New&quot; section for the &#39;</span>
<span class="go">                &#39;most recent version\n&#39;</span>
<span class="go">                &#39;may be appropriate.&#39;,</span>
<span class="go"> &#39;description_content_type&#39;: None,</span>
<span class="go"> &#39;docs_url&#39;: None,</span>
<span class="go"> &#39;download_url&#39;: &#39;UNKNOWN&#39;,</span>
<span class="go"> &#39;downloads&#39;: {&#39;last_day&#39;: -1, &#39;last_month&#39;: -1, &#39;last_week&#39;: -1},</span>
<span class="go"> &#39;home_page&#39;: &#39;https://github.com/pypa/sampleproject&#39;,</span>
<span class="go"> &#39;keywords&#39;: &#39;sample setuptools development&#39;,</span>
<span class="go"> &#39;license&#39;: &#39;MIT&#39;,</span>
<span class="go"> &#39;maintainer&#39;: None,</span>
<span class="go"> &#39;maintainer_email&#39;: None,</span>
<span class="go"> &#39;name&#39;: &#39;sampleproject&#39;,</span>
<span class="go"> &#39;package_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
<span class="go"> &#39;platform&#39;: &#39;UNKNOWN&#39;,</span>
<span class="go"> &#39;project_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
<span class="go"> &#39;project_urls&#39;: {&#39;Download&#39;: &#39;UNKNOWN&#39;,</span>
<span class="go">                  &#39;Homepage&#39;: &#39;https://github.com/pypa/sampleproject&#39;},</span>
<span class="go"> &#39;release_url&#39;: &#39;https://pypi.org/project/sampleproject/1.2.0/&#39;,</span>
<span class="go"> &#39;requires_dist&#39;: None,</span>
<span class="go"> &#39;requires_python&#39;: None,</span>
<span class="go"> &#39;summary&#39;: &#39;A sample Python project&#39;,</span>
<span class="go"> &#39;version&#39;: &#39;1.2.0&#39;}</span>
</pre></div>
</div>
<p>结果可以被限制到特定的 <em>depth</em> (更深层的内容将使用省略号):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">project_info</span><span class="p">,</span> <span class="n">depth</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">{&#39;author&#39;: &#39;The Python Packaging Authority&#39;,</span>
<span class="go"> &#39;author_email&#39;: &#39;pypa-dev@googlegroups.com&#39;,</span>
<span class="go"> &#39;bugtrack_url&#39;: None,</span>
<span class="go"> &#39;classifiers&#39;: [...],</span>
<span class="go"> &#39;description&#39;: &#39;A sample Python project\n&#39;</span>
<span class="go">                &#39;=======================\n&#39;</span>
<span class="go">                &#39;\n&#39;</span>
<span class="go">                &#39;This is the description file for the project.\n&#39;</span>
<span class="go">                &#39;\n&#39;</span>
<span class="go">                &#39;The file should use UTF-8 encoding and be written using &#39;</span>
<span class="go">                &#39;ReStructured Text. It\n&#39;</span>
<span class="go">                &#39;will be used to generate the project webpage on PyPI, and &#39;</span>
<span class="go">                &#39;should be written for\n&#39;</span>
<span class="go">                &#39;that purpose.\n&#39;</span>
<span class="go">                &#39;\n&#39;</span>
<span class="go">                &#39;Typical contents for this file would include an overview of &#39;</span>
<span class="go">                &#39;the project, basic\n&#39;</span>
<span class="go">                &#39;usage examples, etc. Generally, including the project &#39;</span>
<span class="go">                &#39;changelog in here is not\n&#39;</span>
<span class="go">                &#39;a good idea, although a simple &quot;What\&#39;s New&quot; section for the &#39;</span>
<span class="go">                &#39;most recent version\n&#39;</span>
<span class="go">                &#39;may be appropriate.&#39;,</span>
<span class="go"> &#39;description_content_type&#39;: None,</span>
<span class="go"> &#39;docs_url&#39;: None,</span>
<span class="go"> &#39;download_url&#39;: &#39;UNKNOWN&#39;,</span>
<span class="go"> &#39;downloads&#39;: {...},</span>
<span class="go"> &#39;home_page&#39;: &#39;https://github.com/pypa/sampleproject&#39;,</span>
<span class="go"> &#39;keywords&#39;: &#39;sample setuptools development&#39;,</span>
<span class="go"> &#39;license&#39;: &#39;MIT&#39;,</span>
<span class="go"> &#39;maintainer&#39;: None,</span>
<span class="go"> &#39;maintainer_email&#39;: None,</span>
<span class="go"> &#39;name&#39;: &#39;sampleproject&#39;,</span>
<span class="go"> &#39;package_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
<span class="go"> &#39;platform&#39;: &#39;UNKNOWN&#39;,</span>
<span class="go"> &#39;project_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
<span class="go"> &#39;project_urls&#39;: {...},</span>
<span class="go"> &#39;release_url&#39;: &#39;https://pypi.org/project/sampleproject/1.2.0/&#39;,</span>
<span class="go"> &#39;requires_dist&#39;: None,</span>
<span class="go"> &#39;requires_python&#39;: None,</span>
<span class="go"> &#39;summary&#39;: &#39;A sample Python project&#39;,</span>
<span class="go"> &#39;version&#39;: &#39;1.2.0&#39;}</span>
</pre></div>
</div>
<p>此外，还可以设置建议的最大字符 <em>width</em>。 如果一个对象无法被拆分，则将超出指定宽度:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">project_info</span><span class="p">,</span> <span class="n">depth</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span>
<span class="go">{&#39;author&#39;: &#39;The Python Packaging Authority&#39;,</span>
<span class="go"> &#39;author_email&#39;: &#39;pypa-dev@googlegroups.com&#39;,</span>
<span class="go"> &#39;bugtrack_url&#39;: None,</span>
<span class="go"> &#39;classifiers&#39;: [...],</span>
<span class="go"> &#39;description&#39;: &#39;A sample Python project\n&#39;</span>
<span class="go">                &#39;=======================\n&#39;</span>
<span class="go">                &#39;\n&#39;</span>
<span class="go">                &#39;This is the description file for the &#39;</span>
<span class="go">                &#39;project.\n&#39;</span>
<span class="go">                &#39;\n&#39;</span>
<span class="go">                &#39;The file should use UTF-8 encoding and be &#39;</span>
<span class="go">                &#39;written using ReStructured Text. It\n&#39;</span>
<span class="go">                &#39;will be used to generate the project &#39;</span>
<span class="go">                &#39;webpage on PyPI, and should be written &#39;</span>
<span class="go">                &#39;for\n&#39;</span>
<span class="go">                &#39;that purpose.\n&#39;</span>
<span class="go">                &#39;\n&#39;</span>
<span class="go">                &#39;Typical contents for this file would &#39;</span>
<span class="go">                &#39;include an overview of the project, &#39;</span>
<span class="go">                &#39;basic\n&#39;</span>
<span class="go">                &#39;usage examples, etc. Generally, including &#39;</span>
<span class="go">                &#39;the project changelog in here is not\n&#39;</span>
<span class="go">                &#39;a good idea, although a simple &quot;What\&#39;s &#39;</span>
<span class="go">                &#39;New&quot; section for the most recent version\n&#39;</span>
<span class="go">                &#39;may be appropriate.&#39;,</span>
<span class="go"> &#39;description_content_type&#39;: None,</span>
<span class="go"> &#39;docs_url&#39;: None,</span>
<span class="go"> &#39;download_url&#39;: &#39;UNKNOWN&#39;,</span>
<span class="go"> &#39;downloads&#39;: {...},</span>
<span class="go"> &#39;home_page&#39;: &#39;https://github.com/pypa/sampleproject&#39;,</span>
<span class="go"> &#39;keywords&#39;: &#39;sample setuptools development&#39;,</span>
<span class="go"> &#39;license&#39;: &#39;MIT&#39;,</span>
<span class="go"> &#39;maintainer&#39;: None,</span>
<span class="go"> &#39;maintainer_email&#39;: None,</span>
<span class="go"> &#39;name&#39;: &#39;sampleproject&#39;,</span>
<span class="go"> &#39;package_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
<span class="go"> &#39;platform&#39;: &#39;UNKNOWN&#39;,</span>
<span class="go"> &#39;project_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
<span class="go"> &#39;project_urls&#39;: {...},</span>
<span class="go"> &#39;release_url&#39;: &#39;https://pypi.org/project/sampleproject/1.2.0/&#39;,</span>
<span class="go"> &#39;requires_dist&#39;: None,</span>
<span class="go"> &#39;requires_python&#39;: None,</span>
<span class="go"> &#39;summary&#39;: &#39;A sample Python project&#39;,</span>
<span class="go"> &#39;version&#39;: &#39;1.2.0&#39;}</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> --- 数据美化输出</a><ul>
<li><a class="reference internal" href="#prettyprinter-objects">PrettyPrinter 对象</a></li>
<li><a class="reference internal" href="#example">示例</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="copy.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copy</span></code> --- 浅层 (shallow) 和深层 (deep) 复制操作</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="reprlib.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> --- 另一种 <code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code> 实现</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/pprint.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="reprlib.html" title="reprlib --- 另一种 repr() 实现"
             >下一页</a> |</li>
        <li class="right" >
          <a href="copy.html" title="copy --- 浅层 (shallow) 和深层 (deep) 复制操作"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="datatypes.html" >数据类型</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>